package com.example.aaudiodemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.AssetManager;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.example.aaudiodemo.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final int INVALID_PTR = 0;

    // Used to load the 'aaudiodemo' library on application startup.
    static {
        System.loadLibrary("aaudiodemo");
    }

    private ActivityMainBinding binding;
    private long mEngineHandle = INVALID_PTR;
    private static final String TEST_FILE_PATH = "/storage/emulated/0/a.pcm";  // "48k16bit.pcm"
    private static final int AUDIO_SAMPLERATE = 44100;
    private static final int AUDIO_CHANNELS = 1;
    private static final int AUDIO_FORMAT = 2; // 2：AAUDIO_FORMAT_PCM_I16 其他：AAUDIO_FORMAT_UNSPECIFIED

    private long mRecordHandle = INVALID_PTR;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mEngineHandle = nativeCreateAAudioEngine(getAssets(), TEST_FILE_PATH, AUDIO_SAMPLERATE, AUDIO_CHANNELS, AUDIO_FORMAT);
        mRecordHandle = nativeCreateAAudioRecord(TEST_FILE_PATH, AUDIO_SAMPLERATE, AUDIO_CHANNELS, AUDIO_FORMAT);

        binding.initAudio.setOnClickListener(v -> {
            if (mEngineHandle == INVALID_PTR) {
                mEngineHandle = nativeCreateAAudioEngine(getAssets(), TEST_FILE_PATH, AUDIO_SAMPLERATE, AUDIO_CHANNELS, AUDIO_FORMAT);
            }

            if (mRecordHandle == INVALID_PTR) {
                mRecordHandle = nativeCreateAAudioRecord(TEST_FILE_PATH, AUDIO_SAMPLERATE, AUDIO_CHANNELS, AUDIO_FORMAT);
            }

            AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
            String samplerate = am.getProperty(AudioManager.PROPERTY_OUTPUT_SAMPLE_RATE);
            String bufferlen = am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
            Toast.makeText(this, "输出设备采样率为：" + samplerate + " 缓冲区长度：" + bufferlen, Toast.LENGTH_LONG).show();

        });
        binding.playAudio.setOnClickListener((v) -> {
            if (mEngineHandle != INVALID_PTR) {
                nativeAAudioEnginePlay(mEngineHandle);
            }

            if (mRecordHandle != INVALID_PTR) {
                nativeAAudioRecordPlay(mRecordHandle);
            }
        });
        binding.pauseAudio.setOnClickListener((v) -> {
            if (mRecordHandle != INVALID_PTR) {
                nativeAAudioRecordPause(mRecordHandle);
            }

            if (mEngineHandle != INVALID_PTR) {
                nativeAAudioEnginePause(mEngineHandle);
            }
        });
        binding.stopAudio.setOnClickListener((v) -> {
            if (mRecordHandle != INVALID_PTR) {
                nativeAAudioRecordStop(mRecordHandle);
                nativeDestroyAAudioRecord(mRecordHandle);
                mRecordHandle = INVALID_PTR;
            }

            if (mEngineHandle != INVALID_PTR) {
                nativeAAudioEngineStop(mEngineHandle);
                nativeDestroyAAudioEngine(mEngineHandle);
                mEngineHandle = INVALID_PTR;
            }
        });
    }

    public native long nativeCreateAAudioEngine(AssetManager assetMgr, String filePath, int sampleRate, int audioChannel, int audioFormat);

    public native void nativeDestroyAAudioEngine(long engineHandle);

    public native void nativeAAudioEnginePlay(long engineHandle);

    public native void nativeAAudioEnginePause(long engineHandle);

    public native void nativeAAudioEngineStop(long engineHandle);

    /* Record *********************************************************************/
    public native long nativeCreateAAudioRecord(String filePath, int sampleRate, int audioChannel, int audioFormat);

    public native void nativeDestroyAAudioRecord(long engineHandle);

    public native void nativeAAudioRecordPlay(long engineHandle);

    public native void nativeAAudioRecordPause(long engineHandle);

    public native void nativeAAudioRecordStop(long engineHandle);
}